package com.kla.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kla.power.entity.FileEntity;
import com.kla.power.mapper.FileMapper;
import com.kla.power.service.IFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kla.power.util.StateEnum;
import com.kla.power.util.StateException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文件表 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2024-08-06
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, FileEntity> implements IFileService {


    @Override
    public FileEntity saveSingle (FileEntity fileEntity) {
        if ( fileEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        fileEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( fileEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return fileEntity;
    }

    @Override
    public List<FileEntity> queryList ( FileEntity fileEntity) {
        if ( fileEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        List<FileEntity> fileEntities = baseMapper.selectList ( new QueryWrapper<FileEntity> ().eq ( "relationId", fileEntity.getRelationId () ).eq ( "type", fileEntity.getType () ) );
        return fileEntities;
    }

    @Override
    public FileEntity deleteSingle (FileEntity fileEntity) {
        int i = baseMapper.deleteById ( fileEntity );
        if ( i == 0 ) {
            throw new StateException ( StateEnum.DELETE_EXCE );
        }
        return fileEntity;
    }

}
